<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">

      <div class="title-container">
        <img class="title" :src="imgLogo" alt="">
      </div>

      <el-form-item prop="loginName">
        <span class="svg-container">
          <i class="iconfont icon-shoujihao" />
        </span>
        <el-input
          ref="username"
          v-model="loginForm.loginName"
          placeholder="loginName"
          name="loginName"
          type="text"
          maxlength="18"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <i class="iconfont icon-mima" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="Password"
          name="password"
          maxlength="21"
          tabindex="2"
          auto-complete="on"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>
      <el-form-item prop="code">
        <el-col :span="17">
          <span class="svg-container">
            <i class="iconfont icon-yzm" />
          </span>
          <el-input
            ref="keycode"
            v-model="loginForm.code"
            placeholder="请输入验证码"
            name="code"
            maxlength="4"
            type="text"
            @keyup.enter.native="handleLogin"
          /></el-col>
        <el-col :span="7">
          <img
            class="yzm"
            :src="`http://likede2-admin.itheima.net/likede/api/user-service/user/imageCode/${loginForm.clientToken}`"
            alt=""
            @click="imgFn"
          >
        </el-col>

      </el-form-item>

      <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">登录</el-button>
    </el-form>
  </div>
</template>

<script>
import { Message } from 'element-ui'
import imgLogo from '@/assets/logo.png'
export default {
  name: 'Login',
  data() {
    return {
      imgLogo,
      loginForm: {
        loginName: 'admin',
        password: 'admin',
        code: '',
        clientToken: Math.random(),
        loginType: 0
      },
      loginRules: {
        loginName: [
          { required: true, trigger: 'blur', message: '请输入用户名' },
          { min: 5, max: 18, message: '长度在 5 到 18 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, trigger: 'blur', message: '请输入密码' }
        ],
        code: [
          { required: true, trigger: 'blur', message: '请输入验证码' }
        ]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    imgFn() {
      this.loginForm.clientToken = Math.random()
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    // 兜底校验
    handleLogin() {
      this.$refs.loginForm.validate(async valid => {
        if (valid) {
          this.loading = true
          try {
            const res = await this.$store.dispatch('user/login', this.loginForm)
            Message({
              type: res.success ? 'success' : 'error',
              message: res.msg
            })
            this.$router.push('/')
          } catch (error) {
            console.log(error)
          }
          this.loading = false
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: #999;
      height: 47px;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }
  .el-col.el-col-7{
    margin: 0;
    padding: 0;
    height: 53px;
    .yzm{
      vertical-align: middle;
    width: 100%;
    height: 100%;
  }
}
  .el-form-item {
    border: 1px solid #d2d2d2;
    background: rgba(255, 255, 255, 0.1);
    border-radius: 3px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background: url('../../assets/beijingdatu.jpg') no-repeat center;
  background-size:cover;
  overflow: hidden;
    .login-form {
      position: relative;
      width: 520px;
      max-width: 100%;
      padding: 60px 35px 0;
      margin: 0 auto;
      margin-top: 150px;
      border-radius: 5%;
      background-color: #fff;
  }

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      position: absolute;
      top: -110px;
      left: 50%;

      z-index: 99;
      width: 96px;
      height: 96px;
      margin: 0px auto 40px -48px;
      text-align: center;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
.el-button.el-button--primary{
   height: 52px;
   background: linear-gradient(262deg,#2e50e1,#6878f0);
   opacity: .91;
    border-radius: 8px;
    color: #fff;
    text-shadow: 0 7px 22px #cfcfcf;
}
</style>
